package com.sctvcloud.bazhong.ui.adapter.holder;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.beans.HomeListItem;
import com.sctvcloud.bazhong.beans.NewsItem;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.sctvcloud.bazhong.application.Constances.INFORMATION_TYPE_FLASH;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class HomeActiveHolder extends BaseAbsHolder<HomeListItem> implements IAndroidLittleLife, View.OnClickListener {
    @BindView(R.id.home_active_img)
    protected ImageView img;
    @BindView(R.id.home_active_title)
    protected TextSwitcher title;
    private List<NewsItem> data;
    private Handler handler;
    private int currentPos;
    private boolean hasInManager;
    private NewsItem currentActive;
    private Timer timer;
    protected Object synObj = new Object();
    private HomeListItem tempNewsItem;
    private CustomFontTextView tv;

    private boolean isFirst = true;

    public HomeActiveHolder(final Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        title.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                tv = (CustomFontTextView) LayoutInflater.from(context).inflate(R.layout.item_home_actives_title, null);
//                JLog.e("okh  tv=" + tv);
                return tv;
            }
        });
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(HomeListItem newsItem) {
        // newsItem.setSubType(1);
        if (isSameData(newsItem)) {
            return;
        }
//        JLog.e("okh  " + newsItem);
        if (newsItem.getSubType() == INFORMATION_TYPE_FLASH) {
//            JLog.e("okh int flash");
            isFirst = false;
            img.setImageResource(R.mipmap.icon_flash);
//            tv.setEllipsize(TextUtils.TruncateAt.END);
            setViewToFlash((TextView) title.getCurrentView());
            setViewToFlash((TextView) title.getNextView());
            title.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.switcher_anim_in));
            title.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.switcher_anim_out));
        } else if (newsItem.getSubType() == Constances.INFORMATION_TYPE_WEIGHT) {
//            JLog.e("okh int weight  curView=" + title.getCurrentView() + "\ntv=" + tv + "\n" + title.getNextView());
            img.setImageResource(R.mipmap.icon_home_heavy);
            setViewToWeight((TextView) title.getCurrentView());
            setViewToWeight((TextView) title.getNextView());

        }
        initHandler();
        data = newsItem.getNewsItemList();
        playActive(newsItem);
    }

    private void setViewToWeight(TextView tvw){
        if (tvw==null){
            return;
        }
        tvw.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        tvw.setMarqueeRepeatLimit(-1);
        tvw.setSelected(true);
    }

    private void setViewToFlash(TextView tvw){
        if (tvw==null){
            return;
        }
        tvw.setEllipsize(TextUtils.TruncateAt.END);
    }

    private void resetToWeight() {
//        JLog.e("okh  cur=" + title.getCurrentView() + "\n   tv=" + tv+"\nf="+isFirst+"  "+title.getNextView());
        if (isFirst){
            isFirst=false;
        }else {
            tv = (CustomFontTextView) title.getNextView();
        }
        tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        tv.setMarqueeRepeatLimit(-1);
        tv.setSelected(true);
    }

    private void initHandler() {
        if (handler == null) {
            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    nextPlay();
                }
            };
        }
    }

    private void startPlay() {
        initHandler();
        if (timer == null) {
        }
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }, 2000, 2000);
    }

    private void stopPlay() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void nextPlay() {
        synchronized (synObj) {
            currentPos++;
            if (currentPos == data.size()) {
                currentPos = 0;
            }
            currentActive = data.get(currentPos);
            setData(currentActive.getNewsTitle());
        }
    }

    private void setData(String titles) {
        if (!TextUtils.isEmpty(titles)) {
            title.setText(titles);
        }
    }

    private boolean isSameData(@NonNull HomeListItem item) {
        if (this.tempNewsItem == null) {
            return false;
        }
        if (item.getSubType() == tempNewsItem.getSubType()) {
            List<NewsItem> l = item.getNewsItemList();
            List<NewsItem> t = tempNewsItem.getNewsItemList();
            if (l == null) {
                if (t == null) {
                    return true;
                }
            } else {
                if (t != null) {
                    if (t.size() == l.size()) {
                        if (t.size() == 0) {
                            return true;
                        }
                        if (item.getSubType() == Constances.INFORMATION_TYPE_FLASH) {
                            if (t.size() <= 3) {
                                boolean isNot = false;
                                for (int i = 0; i < t.size(); i++) {
                                    NewsItem ni = t.get(i);
                                    int j = 0;
                                    for (; j < l.size(); j++) {
                                        if (ni.isSigledSame(l.get(j))) {
                                            break;
                                        }
                                    }
                                    if (j >= l.size()) {
                                        isNot = true;
                                        break;
                                    }
                                }
                                return !isNot;
                            }
                        } else if (item.getSubType() == Constances.INFORMATION_TYPE_WEIGHT) {
                            return t.get(0).isSigledSame(l.get(0));
                        }
                    }
                }
            }

        }
        return false;
    }

    private void playActive(HomeListItem newsItem) {
//        JLog.e("eese newsitem=" + newsItem + "\n " + this);
        if (newsItem.getNewsItemList().size() == 1) {
            stopPlay();
            currentActive = newsItem.getNewsItemList().get(0);
            currentPos = 0;
            setData(currentActive.getNewsTitle());
//            JLog.e("====titkle==" + currentActive.getNewsTitle());
//            resetToWeight();
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                }
//            }, 500);

        } else {
            synchronized (synObj) {
                if (timer == null) {
                    currentActive = newsItem.getNewsItemList().get(currentPos);
                    if (newsItem != tempNewsItem) {
                        currentPos = -1;
                    }
                    startPlay();
                    nextPlay();
                } else {
                    if (newsItem != tempNewsItem) {
                        currentPos = 0;
                    }
                    currentActive = newsItem.getNewsItemList().get(currentPos);
                    setData(currentActive.getNewsTitle());
                }
            }
        }
        tempNewsItem = newsItem;
//        JLog.e("eese  finally  temp=" + tempNewsItem + "\n    newsitem=" + newsItem + "\n  " + this);
    }


    @Override
    public int onIResume() {
        return 0;
    }

    @Override
    public int onIPause() {
        return 0;
    }

    @Override
    public int onIDestory() {
        return 0;
    }

    @Override
    public void setHasInManager(boolean inManager) {
        this.hasInManager = inManager;
    }

    @Override
    public boolean hasInManager() {
        return hasInManager;
    }

    @Override
    public void setLifeMode(int mode) {

    }

    @Override
    public void onClick(View view) {
        if (internalClick != null) {
            view.setTag(R.id.adapter_item_data_key, tempNewsItem);
            internalClick.onItemInternalClick(itemView, view, currentPos);
        }
    }

}

